package com.skl.base.model;

import java.lang.ref.WeakReference;

/**
 * 应用模块: model
 * 类描述: 通用的基类model
 */
public abstract class BaseModel<T>  extends SuperBaseModel<T>{

    @Override
    protected void notifyCacheData(T data) {
        loadSuccess(data);
    }

    /**
     * 网络数据加载成功,通知所有注册者加载数据
     * @param data
     */
    public  void loadSuccess(T data){
        synchronized (this) {
            mUIHandler.postDelayed(() -> {
                for (WeakReference<IBaseModelListener> weakListener : mWeakReferenceDeque) {
                    if (weakListener.get() instanceof IModelListener){
                        IModelListener listenerItem = (IModelListener)weakListener.get();
                        if (listenerItem != null) {
                            listenerItem.onLoadFinish(BaseModel.this, data);
                        }
                    }
                }
            }, 0);
        }
    }

    /**
     * 数据加载失败,通知所有注册者
     */
    protected void loadFail(String prompt) {
        synchronized (this) {
            mUIHandler.postDelayed(() -> {
                for (WeakReference<IBaseModelListener> weakListener : mWeakReferenceDeque) {
                    if (weakListener.get()instanceof IModelListener) {
                        IModelListener listenerItem = (IModelListener) weakListener.get();
                        if (listenerItem != null) {
                            listenerItem.onLoadFail(BaseModel.this, prompt);
                        }
                    }
                }
            }, 0);
        }
    }
}
